﻿#include <QFileDialog>
#include <QToolBar>
#include <QVBoxLayout>
#include <Configuration.h>
#include <Constants.h>
#include <Resources.h>
#include <FileSystemTreeWidget.h>
#include <FileSystemTreeWidgetItem.h>
#include "WorkspaceExplorerWindow.h"

namespace Domy {

WorkspaceExplorerWindow::WorkspaceExplorerWindow(ActionEnableCtrl ctrl, QWidget *parent) :
    DockWidgetInterface(ctrl, parent),
    m_is_open(false),
    m_tool_bar(new QToolBar(this)),
    m_tree_widget(new FileSystemTreeWidget(this)),
    m_act_open(Q_NULLPTR),
    m_act_close(Q_NULLPTR)
{
    m_tool_bar->setIconSize(QSize(16, 16));

    QVBoxLayout *l = new QVBoxLayout(this);
    l->addWidget(m_tool_bar, 0);
    l->addWidget(m_tree_widget, 1);
    l->setContentsMargins(5, 0, 0, 0);
    l->setSpacing(0);
    this->setLayout(l);

    m_act_open = m_tool_bar->addAction(GET_ICON("open_folder"), tr("Open Dir ..."));
    connect(m_act_open, SIGNAL(triggered(bool)), this, SLOT(slotOnOpenBtnClicked()));

    m_act_close = m_tool_bar->addAction(GET_ICON("close_folder"), tr("Close Dir ..."));
    m_act_close->setEnabled(false);
    connect(m_act_close, SIGNAL(triggered(bool)), this, SLOT(slotOnCloseBtnClicked()));

    m_tree_widget->setHeaderContent(tr("Workspace"), GET_ICON("workspace"));

    openWorkspace(domy_config->getSettingsValue(SettingsGroup_Workspace, SettingsKey_WorkspaceDir).toString());

    connect(m_tree_widget, SIGNAL(signalOpenFile(const QString &)), this, SIGNAL(signalOpenFile(const QString &)));
}

WorkspaceExplorerWindow::~WorkspaceExplorerWindow()
{

}

QDockWidget *WorkspaceExplorerWindow::createDockWidget()
{
    QDockWidget *dock_widget = new QDockWidget(tr("Workspace"), parentWidget());
    dock_widget->setObjectName("WorkspaceExplorerWindow");
    dock_widget->setFeatures(dock_widget->features() & ~QDockWidget::DockWidgetFloatable);
    dock_widget->setWidget(this);

    return dock_widget;
}

void WorkspaceExplorerWindow::slotOnOpenBtnClicked()
{
    QString dir_path = QFileDialog::getExistingDirectory(this, tr("Open Workspace..."), domy_config->getLastOpenDirPath());

    openWorkspace(dir_path);
}

void WorkspaceExplorerWindow::slotOnCloseBtnClicked()
{
    m_tree_widget->clear();
    m_tree_widget->setHeaderText(tr("Workspace"));

    m_is_open = false;

    m_act_close->setEnabled(false);

    domy_config->setSettingsValue(SettingsGroup_Workspace, SettingsKey_WorkspaceDir, "");
}

void WorkspaceExplorerWindow::openWorkspace(const QString &dir_path)
{
    if (dir_path.isEmpty()) {
        return;
    }

    QDir dir(dir_path);
    if (!dir.exists()) {
        return;
    }

    m_tree_widget->clear();
    m_tree_widget->setHeaderText(dir.dirName());

    QFileInfoList fi_list = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name | QDir::DirsFirst);

    m_tree_widget->add(fi_list);

    m_is_open = true;
    m_act_close->setEnabled(true);

    domy_config->setSettingsValue(SettingsGroup_Workspace, SettingsKey_WorkspaceDir, dir_path);
}

} // namespace Domy
